const model = require('../model')
const r=require('../dto/resp')
var main=require('../../comic')
var fetchTopic = async ctx=>{
  let {code}=ctx.params
  let topic = await model.ComicTopic.getBy({code})
  ctx.body=r().setItemMap({topic})
};
var fetchChapters = async ctx=>{
  console.log(ctx.params)
  let {topic}=ctx.params
  let chapters = await model.ComicChapter.findBy({topic})
  ctx.body=r().setItems(chapters)
};
var fetchContents = async ctx=>{
  console.log(ctx.params)
  let {topic,cid}=ctx.params
  let chapter=await model.ComicChapter.getBy({cid})
  let next=await model.ComicChapter.getBy({num:chapter.num+1,topic:chapter.topic})
  let contents = await model.ComicContent.findBy({cid,topic})
  ctx.body=r().setItems(contents).setItemMap({chapter,next})
};
var captureContents = async ctx=>{
  console.log(ctx.params)
  let {topic,cid}=ctx.params
  let chapter=await model.ComicChapter.getBy({cid})
  let contents =await main.getChapter(chapter)
  
  ctx.body=r().setItems(contents).setItemMap({chapter})
};



module.exports = {
  'GET /topics/:code': fetchTopic,
  'GET /topics/:topic/chapters': fetchChapters,
  'GET /topics/:topic/chapters/:cid/contents': fetchContents,
  'GET /topics/:topic/chapters/:cid/capture': captureContents,
  

};